<template>
  <div class="m-l-50 m-t-30 w-500">
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-form-item label="显示名" prop="title">
        <el-input v-model.trim="form.title" class="h-40 w-200"></el-input>
      </el-form-item>
      <el-form-item label="父权限" prop="pid">
        <el-select v-model="form.pid" placeholder="父权限" class="w-200" v-on:change="selectPid()">
          <el-option v-for="item in ruleOptions" :label="item.title" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模块" prop="module">
        <el-input v-model.trim="form.module" class="h-40 w-200" :disabled="isModuleDisable"></el-input>
      </el-form-item>
      <el-form-item label="控制器" prop="controller" >
        <el-input v-model.trim="form.controller" class="h-40 w-200" :disabled="isControllerDisable"></el-input>
      </el-form-item>
      <el-form-item label="方法" prop="action">
        <el-input v-model.trim="form.action" class="h-40 w-200" :disabled="isActionDisable"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="edit('form')" :loading="isLoading">提交</el-button>
        <el-button @click="goback()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import http from '../../../assets/js/http'
  import fomrMixin from '../../../assets/js/form_com'
  import _g from '@/assets/js/global'
  import { Button, Form, FormItem, Input, Select, Option, Radio, RadioGroup } from 'element-ui'
  import _ from 'lodash'
  Vue.use(Button)
  Vue.use(Form)
  Vue.use(FormItem)
  Vue.use(Input)
  Vue.use(Select)
  Vue.use(Option)
  Vue.use(Radio)
  Vue.use(RadioGroup)

  export default {
    data() {
      return {
        isLoading: false,
        form: {
          id: null,
          title: '',
          pid: null,
          module: '*',
          controller: '*',
          action: '*'
        },
        isActionDisable: false,
        isControllerDisable: false,
        isModuleDisable: false,
        ruleOptions: [{ rule_id: 0, id: 0, title: '根节点', module: '*', controller: '*', action: '*' }],
        rules: {
          title: [
            { required: true, message: '请输入名称' }
          ],
          module: [
            { required: true, message: '请输入模块' }
          ],
          controller: [
            { required: true, message: '请输入控制器' }
          ],
          action: [
            { required: true, message: '请输入方法' }
          ],
          pid: [
            { type: 'number', required: true, message: '请选择父级节点' }
          ]
        }
      }
    },
    methods: {
      selectPid() {
        let i = 0
        for (i = 0; i < this.ruleOptions.length; i++) {
          if (this.ruleOptions[i].rule_id == this.form.pid) {
            this.form.module = '*'
            this.isModuleDisable = false
            if (this.ruleOptions[i].module !== '*') {
              this.form.module = this.ruleOptions[i].module
              this.isModuleDisable = true
            }
            this.form.controller = '*'
            this.isControllerDisable = false
            if (this.ruleOptions[i].controller !== '*') {
              this.form.controller = this.ruleOptions[i].controller
              this.isControllerDisable = true
            }
            this.form.action = '*'
            this.isActionDisable = false
            if (this.ruleOptions[i].action !== '*') {
              this.form.action = this.ruleOptions[i].action
              this.isActionDisable = true
            }
            break
          }
        }
      },
      edit(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            this.isLoading = true
            if (this.$route.params.id) {
              this.apiPut('admin/memberRule/', this.form.id, this.form).then((res) => {
                this.handelResponse(res, (data) => {
                  this.isLoading = false
                  _g.toastMsg('success', '编辑成功')
                  setTimeout(() => {
                    this.goback()
                  }, 1500)
                }, () => {
                  this.isLoading = false
                })
              })
            } else {
              this.apiPost('admin/memberRule', this.form).then((res) => {
                this.handelResponse(res, (data) => {
                  this.isLoading = false
                  _g.toastMsg('success', '添加成功')
                  setTimeout(() => {
                    this.goback()
                  }, 1500)
                }, () => {
                  this.isLoading = false
                })
              })
            }
          }
        })
      },
      getRules() {
        this.apiGet('admin/memberRule', { 'limit': 500 }).then((res) => {
          this.handelResponse(res, (data) => {
            _(data.list).forEach((ret) => {
              if (ret.action === '*') {
                this.ruleOptions.push(ret)
              }
            })
          })
        })
      },
      getRuleInfo() {
        this.form.id = this.$route.params.id
        this.apiGet('admin/memberRule/' + this.form.id).then((res) => {
          this.handelResponse(res, (data) => {
            data.level = data.level.toString()
            this.form = data
          })
        })
      }
    },
    created() {
      this.getRules()
      if (this.$route.params.id) {
        this.getRuleInfo()
      }
    },
    mixins: [http, fomrMixin]
  }
</script>
